package com.ganxiang.ximenhong.widght;


import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import com.ganxiang.ximenhong.R;

public class MyWebView extends LinearLayout {

	private String mUrl;
	private View mView;
	private WebView mWebView;
	private ColorProgressBar mColorProgressBar;

	public MyWebView(Context context, String url) {
		super(context);
		this.mUrl = url;
		mView = View.inflate(context, R.layout.view_webview, null);
		this.addView(mView);
		initWebView();
	}
	
	@SuppressLint("SetJavaScriptEnabled")
	private void initWebView() {
		mWebView = (WebView) mView.findViewById(R.id.webView);
		mColorProgressBar = (ColorProgressBar) mView.findViewById(R.id.cpb);
		mWebView.getSettings().setJavaScriptEnabled(true);
		mWebView.setWebChromeClient(new WebChromeClient() {
			@Override
			public void onProgressChanged(WebView view, int progress) {
				mColorProgressBar.setProgress(progress);
				if (progress == 100) {
					mColorProgressBar.setVisibility(View.GONE);
				}
			}
		});
		mWebView.setWebViewClient(new WebViewClient() {
			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				mColorProgressBar.setVisibility(View.GONE);

			}

			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				mColorProgressBar.setVisibility(View.GONE);
			}

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				
				return true;
			}
		});
		mWebView.loadUrl(mUrl);
	}

}
